package util;

import javax.swing.table.AbstractTableModel;

import data.EVEItem;
import data.EVESystem;
import data.ItemSystemCollection;

public class ItemMarketOrderModel extends AbstractTableModel {

  private ItemSystemCollection _collection;
  private EVEItem              _item;
  private boolean              _nullItem;

  public ItemMarketOrderModel(ItemSystemCollection collection, EVEItem item) {
    if (collection != null) {
      _collection = collection;
      _item = item;
      _nullItem = false;
    }
    else {
      _nullItem = true;
    }
  }

  @Override
  public int getColumnCount() {
    int numberOfMarketValues = 3;
    return numberOfMarketValues;
  }

  @Override
  public String getColumnName(int columnIndex) {
    if (columnIndex == 0) return "System";
    if (columnIndex == 1) return "Sell order top";
    if (columnIndex == 2) return "Buy order top";
    // if (columnIndex == 1) return "Volume";
    // if (columnIndex == 2) return "Average";
    // if (columnIndex == 3) return "Maximum";
    // if (columnIndex == 4) return "Minimum";
    // if (columnIndex == 5) return "Standard Deviation";
    // if (columnIndex == 6) return "Median";
    // if (columnIndex == 7) return "Percentil";
    return null;
  }

  @Override
  public int getRowCount() {
    if (_nullItem) {
      return 0;
    }
    else {
      return _collection.getSystems().size();
    }
  }

  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
    if (_isValidTablePosition(rowIndex, columnIndex)) {
      EVESystem system = (EVESystem) _collection.getSystems().toArray()[rowIndex];
      if (columnIndex == 0) return system.getSystemName();
      if (columnIndex == 1) return Constants._addDotsToNumber(_item.getItemMarketDataMap().get(system)._sellValues.getMin());
      if (columnIndex == 2) return Constants._addDotsToNumber(_item.getItemMarketDataMap().get(system)._buyValues.getMax());
    }
    return null;
  }

  private boolean _isValidTablePosition(int rowIndex, int columnIndex) {
    if (_nullItem | rowIndex > getRowCount() | columnIndex >= getColumnCount() | rowIndex < 0 | columnIndex < 0) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    String dumpString;
    if (_nullItem) {
      dumpString = "unknown";
    }
    else {
      dumpString = _item._dumpToString();
    }
    return dumpString;
  }
}
